<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div>
        <h1>The AJax 02 Page</h1>
        <button onclick="doAjaxGet()">Do Ajax Get</button>
        <button onclick="doAjaxPost()">Do Ajax Post</button>
        <button onclick="doAjaxDelete()">Do Ajax Delete</button>
        <button onclick="doAjaxPut()">Do Ajax Put</button>
    </div>
    <div id="result"></div>
    <script>
        //Ajax Get 请求方式应用 此方式主要用于从服务端获取数据
        function doAjaxGet() {
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange=function () {
                if(xhr.readyState==4&&xhr.status==200) {
                    document.getElementById("result").innerText = xhr.responseText;
                }
            }
            xhr.open("get","/doAjaxGet",true);
            xhr.send(null);
        }
        //Ajax Post 请求方式应用 此方式主要用于从服务端获取数据
        function doAjaxPost() {
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange=function () {
                if(xhr.readyState==4&&xhr.status==200) {
                    document.getElementById("result").innerText = xhr.responseText;
                }
            }
            xhr.open("POST","/doAjaxPost",true);
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.send("id=200&name=xiaomi&remark=very good");
        }

        function doAjaxDelete() {
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange=function () {
                if(xhr.readyState==4&&xhr.status==200) {
                    document.getElementById("result").innerText = xhr.responseText;
                }
            }
            xhr.open("delete","/doAjaxDelete?id=200",true);
            xhr.send(null);
        }

        function doAjaxPut() {
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange=function () {
                if(xhr.readyState==4&&xhr.status==200) {
                    document.getElementById("result").innerText = xhr.responseText;
                }
            }
            xhr.open("put","/doAjaxPut",true);
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xhr.send("id=200&name=oppo&remark=nice");
        }
    </script>
<!--    <form action="" enctype="application/x-www-form-urlencoded"></form>-->
</body>
</html>